package cn.melonlib.cores.commons.comp.web.monitor;

import cn.melonlib.cores.aop.comp.config.SystemConfig;
import cn.melonlib.cores.commons.comp.dao.DatabaseConfig;
import cn.melonlib.cores.commons.comp.service.SystemConfigReaderService;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.security.auth2.comp.config.ApiConfig;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SystemStatusMonitor extends ApplicationObjectSupport {

    @Resource
    private ApiConfig apiConfig;

    @Resource
    private SystemConfig systemConfig;




    @Resource
    private SystemConfigReaderService systemConfigReaderService;

//    @GetMapping(path = {"/livez","/version"},produces = {APPLICATION_JSON_UTF8_VALUE})
    public String livez() throws Exception{
        return new JsonMap<>().append("status",true)
                .append("system_name",systemConfig.getName())
                .append("system_label",systemConfig.getLabel())
                .append("version",systemConfig.getVersion())
                .append("last_package_date_time", systemConfig.getPackageDateTime())
                .append("now", DateFormatUtils.format(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss.SSSS"))
                .append("msg","系统正常运行。").jsonString();
    }

//    @GetMapping(path = "/readyz",produces = {APPLICATION_JSON_UTF8_VALUE})
    public String readyz() throws Exception{
        return JsonMap.createMap().append("status",true).append("msg","系统正在提供服务。").jsonString();
    }

//    @GetMapping(path = {"/register.api"},produces = {APPLICATION_JSON_UTF8_VALUE})
    public String register(HttpServletRequest request, HttpServletResponse response) throws Exception{
        return systemConfigReaderService.getConfigInfo().jsonString();

    }
}
